package com.maserb.monitor.dataCenter;

import com.maserb.monitor.dataCenter.communication.EigenValue;
import com.maserb.monitor.dataCenter.communication.EigenValueNetPackage;
import com.maserb.monitor.dataCenter.communication.EigenValueRecord;
import com.maserb.monitor.dataCenter.communication.WaveNetPackage;
import com.maserb.monitor.dataCenter.handler.EigenValueRepositoryHandler;
import com.maserb.monitor.dataCenter.handler.EigenValueRequestHandler;
import com.maserb.monitor.dataCenter.handler.WaveRepositoryHandler;
import com.maserb.monitor.dataCenter.handler.WaveRequestHandler;
import com.maserb.monitor.dataCenter.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataManager {
    private String RealCacheKey = "RealCache";
    private String EigenValueCacheKey = "EigenValue";
    private String WaveCacheKey = "Wave";

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private EigenValueRequestHandler eigenValueRequestHandler;

    @Autowired
    private WaveRequestHandler waveRequestHandler;

    @Autowired
    private EigenValueRepositoryHandler eigenValueRepositoryHandler;

    @Autowired
    private WaveRepositoryHandler waveRepositoryHandler;

    @PostConstruct
    private void init() {
        Map<EigenValueRequestKey, EigenValueResponseResult> eigenValueMap = new HashMap<>();
        Map<WaveRequestKey, WaveNetPackage> waveMap = new HashMap<>();
        cacheManager.getCache(RealCacheKey).put(EigenValueCacheKey, eigenValueMap);
        cacheManager.getCache(RealCacheKey).put(WaveCacheKey, waveMap);
    }

    public void addEigenValues(EigenValueNetPackage eigenValueNetPackage) {
        String deviceCode = eigenValueNetPackage.getDeviceCode();
        for (EigenValueRecord record : eigenValueNetPackage.getEigenvalueRecords()) {
            for (EigenValue val : record.getEigenValueList()) {
                EigenValueRequestKey key = new EigenValueRequestKey(deviceCode, record.getPointCode(), val.getValueType());
                String sampleTime = record.getSampleTime().toDateTime();
                EigenValueResponseResult result = new EigenValueResponseResult(val.getEigenValue(), record.getPointCode(),
                        new Object[]{sampleTime, val.getEigenValue()}, val.getWarningStatusEnum(),
                        sampleTime);

//                getEigenValueMap().put(key, result); //缓存暂时用不上
                EigenValueResponse response = new EigenValueResponse(key, result);
                eigenValueRequestHandler.publish(response);
            }
        }

        eigenValueRepositoryHandler.publish(eigenValueNetPackage);
    }

    public void addWave(WaveNetPackage wavePack) {
        String deviceCode = wavePack.getDeviceCode();
        for (EigenValue val : wavePack.getEigenValueList()) {
            EigenValueRequestKey key = new EigenValueRequestKey(deviceCode, wavePack.getPointCode(), val.getValueType());
            String sampleTime = wavePack.getEigenValueTime().toDateTime();
            EigenValueResponseResult result = new EigenValueResponseResult(val.getEigenValue(), wavePack.getPointCode(),
                    new Object[]{sampleTime, val.getEigenValue()}, val.getWarningStatusEnum(),
                    sampleTime);

//                getEigenValueMap().put(key, result); //缓存暂时用不上
            EigenValueResponse response = new EigenValueResponse(key, result);
            eigenValueRequestHandler.publish(response);
        }

        WaveRequestKey key = new WaveRequestKey(deviceCode, wavePack.getPointCode());
        waveRequestHandler.publish(key, wavePack);

        waveRepositoryHandler.publish(wavePack);
    }

    private Map<EigenValueRequestKey, EigenValueResponseResult> getEigenValueMap() {
        return (Map<EigenValueRequestKey, EigenValueResponseResult>) cacheManager.getCache(RealCacheKey).get(EigenValueCacheKey).get();
    }

    private Map<WaveRequestKey, WaveNetPackage> getWaveMap() {
        return (Map<WaveRequestKey, WaveNetPackage>) cacheManager.getCache(WaveCacheKey).get(WaveCacheKey).get();
    }
}